package com.hunan.api.catv.mq.consumer;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonObject;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.po.MemberAccountRelated;
import com.hunan.api.catv.entity.po.PrizeReceiveRecord;
import com.hunan.api.catv.entity.po.PrizeStore;
import com.hunan.api.catv.entity.vo.MemberVo;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.tencent_cloud.sms.SmsBiz;
import com.hunan.api.catv.service.impl.member.MemberService;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ReceiveIqyiCosumer {

    public static final Logger log = LoggerFactory.getLogger(ReceiveIqyiCosumer.class);
    private static final String IQYI_RIGHT_TEMPLATEID = "960030";


    @Autowired
    RedisUtils redisUtils;

    @Autowired
    MemberService memberService;

    @Autowired
    SmsBiz smsBiz;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.RECEIVE_IQIYI_SEND_SMS_QUEUE, durable = "true"), exchange = @Exchange(value = MqConstants.RECEIVE_IQIYI_SEND_SMS_EXCHANGE, durable = "true", type = "topic"), key = MqConstants.RECEIVE_IQIYI_SEND_SMS_ROUTINGKEY))
    public void tasks(String msg) {
        log.info("[扫码绑定领取爱奇艺权益收到任务消息:{}]", msg);
        PrizeReceiveRecord prizeReceiveRecord = GsonUtils.fromJson(msg, PrizeReceiveRecord.class);
        MemberAccountRelated memberAccountRelated = new MemberAccountRelated().selectOne(new QueryWrapper<MemberAccountRelated>().eq(MemberAccountRelated.MEMBER_ID, prizeReceiveRecord.getMemberId()).eq(MemberAccountRelated.TYPE, 2).eq(MemberAccountRelated.STATUS, 1));
        JsonObject jsonObject = new JsonObject();
        if (memberAccountRelated != null) {
            prizeReceiveRecord.setAccountId(memberAccountRelated.getAccountId());
        }
        MemberVo memberVo = memberService.getById(prizeReceiveRecord.getMemberId());

        if (memberVo != null) {
            jsonObject.addProperty("nickName", memberVo.getNickName());
        }
        prizeReceiveRecord.setPrizeSnapshot(jsonObject.toString());
        prizeReceiveRecord.insert();
        log.info("领取爱奇艺权益发送短信：" + memberVo.getPhone());
        PrizeStore prizeStore = new PrizeStore().selectOne(new QueryWrapper<PrizeStore>().eq(PrizeStore.ID, prizeReceiveRecord.getPrizeId()));
        JSONObject extend = JSONUtil.parseObj(prizeStore.getExtend());
        String params = concatParams(extend.get("activationCode").toString(), extend.get("checkCode").toString(), prizeStore.getLinks());
        log.info("领取爱奇艺权益发送短信参数：" + params);
        ServiceResult result = smsBiz.sendSms(memberVo.getPhone(), IQYI_RIGHT_TEMPLATEID, params);
        log.info("领取爱奇艺权益短信返回：" + GsonUtils.toJson(result));
    }

    private String concatParams(String activationCode, String checkCode, String links) {
        return String.format("激活码：%s \n" +
                        "校验码：%s\n" +
                        "激活链接：%s ",
                activationCode,
                checkCode,
                links);
    }


}
